package com.harry.editsearchhistory.util;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Martin-harry
 * @date 2022/3/2
 * @address
 * @Desc ActivityControler
 */
public class ActivityControler {
    public static List<Activity> activities=new ArrayList<>();

    public static void addActivity(Activity activity){
        activities.add(activity);
    }

    public static void removeActivity(Activity activity){
        activities.remove(activity);
    }

    public static void finishAll(){
        for (Activity activity:activities){
            if (!activity.isFinishing()){
                activity.finish();
            }
        }
    }

    /**获取Activity栈中的栈顶的Activity * 需要注意的是，栈是先进后出，所以最上面的Activity是集合中的最后一个*/
    public static Activity getTopActivity(){
        if (activities.isEmpty()){
            //Activity栈为空
            return null;
        }else{
            //不为空时
            return activities.get(activities.size() - 1);
        }
    }

    public static int getActivity(){
        return activities.size();
    }
}
